package com.nari.ies.ne.nw.server.service.fileDataCheck.impl;

import com.nari.ies.ne.nw.server.entity.FileDataCheckEntity;
import com.nari.ies.ne.nw.server.mapper.FileDataCheckMapper;
import com.nari.ies.ne.nw.server.mapper.PlantDataMapper;
import com.nari.ies.ne.nw.server.service.fileDataCheck.ISYZXXFileDataCheckService;
import com.nari.ies.ne.nw.server.util.DataUtil;
import com.nari.ies.ne.nw.server.util.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SYZXXFileDataCheckServiceImpl implements ISYZXXFileDataCheckService {

    @Autowired
    private PlantDataMapper plantDataMapper;

    @Autowired
    private FileDataCheckMapper fileDataCheckMapper;

    @Override
    public void FDSYZXXFileDataCheck(File file) {
//        System.out.println("数据评分--------------------" + file.getName());
        log.info("数据评分--------------------" + file.getName());
        FileDataCheckEntity fileDataCheckEntity;
        List<FileDataCheckEntity> dataList = new ArrayList<>();
        String plantId;//场站id
        Date forecastDate = null;//预测时间
        Date createDate = new Date();//创建时间
        String plantType;//场站类型
        String fileType;//文件类型
        String dataType;//字段类型
        int activePowerNoData = 0;//并网点有功功率无数据
        int activePowerQualifiedData = 0;//并网点有功功率合格数据(有数据且合格)
        int activePowerUnqualifiedData = 0;//并网点有功功率不合格数据(有数据但不合格)
        int reactivePowerNoData = 0;//并网点无功功率无数据
        int reactivePowerQualifiedData = 0;//并网点无功功率合格数据(有数据且合格)
        int reactivePowerUnqualifiedData = 0;//并网点无功功率不合格数据(有数据但不合格)
        int powerFactorNoData = 0;//并网点功率因数无数据
        int powerFactorQualifiedData = 0;//环境温度合格数据(有数据且合格)
        int powerFactorUnqualifiedData = 0;//环境温度不合格数据(有数据但不合格)
        int wireActivePowerNoData = 0;//集电线有功功率无数据
        int wireActivePowerQualifiedData = 0;//有功功率合格数据(有数据且合格)
        int wireActivePowerUnqualifiedData = 0;//有功功率不合格数据(有数据但不合格)

        //表名
        String tableName = "HISDB.NW_NE_FILE_DATACHECK_FD_SYZXX";
        // 并网点数据类型
        int bwdType = 0;
        // 并网点数据计数
        int bwdCount = 0;
        // 集电线数据类型
        int jdxType = 0;
        // 集电线数据计数
        int jdxCount = 0;

        // 文件名称 FD_GD.GDZJFSFD_CDQYC_20230612_073000.dat
        String fileName = file.getName();

        String[] fileNameArr = fileName.split("_");
        // 文件类型
        fileType = fileNameArr[2];
        // 场站类型
        plantType = fileNameArr[0];
        // 场站标识
        String plantCode = fileNameArr[1].split("\\.")[1];

        //String nwCode = fileNameArr[0] + "_" + fileNameArr[1].split("\\.")[0] + ".";
        List<Map<String, Object>> idList = plantDataMapper.selectPlantData(plantCode);

        plantId = idList.get(0).get("ID").toString();
        if (plantId.contains("-")) {
            plantId = plantId.split("-")[0];
        }

        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            // 读取到的每一行数据
            String line = "";

            while ((line = br.readLine()) != null) {
                if (line.contains("<BWD::")) {
                    bwdType = 1;
                    // 获取时间
                    forecastDate = DataUtil.queryTime(line);
                }
                if (line.contains("</BWD::")) {
                    bwdType = 0;
                }

                if (line.contains("<JDX::")) {
                    jdxType = 1;
                }

                if (line.contains("</JDX::")) {
                    jdxType = 0;
                }
                String[] strs = line.split("\t");

                // 数据行
                if (line.contains("#") && bwdType == 1) {
                    bwdCount++;
                    // 并网点有功功率
                    if (!" ".equals(strs[12]) && !"null".equals(strs[12]) && !"NULL".equals(strs[12])) {
                        activePowerQualifiedData++;
                    }

                    // 并网点无功功率(无要求)
                    if (!" ".equals(strs[13]) && !"null".equals(strs[13]) && !"NULL".equals(strs[13])) {
                        reactivePowerQualifiedData++;
                    }

                    // 并网点功率因数(无要求)
                    if (!" ".equals(strs[14]) && !"null".equals(strs[14]) && !"NULL".equals(strs[14])) {
                        powerFactorQualifiedData++;
                    }
                }

                if (line.contains("#") && jdxType == 1) {
                    jdxCount++;
                    // 集电线有功功率
                    if (!("NULL".equals(strs[4]) || " ".equals(strs[4]) || "null".equals(strs[4]))) {
                        wireActivePowerQualifiedData++;
                    }
                }
            }
            // 判断是否每个并网点都合格
            //并网点有功功率
            if (bwdCount > activePowerQualifiedData) {
                activePowerQualifiedData = 0;
            }else {
                activePowerQualifiedData = 1;
            }
            //并网点无功功率
            if (bwdCount > reactivePowerQualifiedData) {
                reactivePowerQualifiedData = 0;
            }else {
                reactivePowerQualifiedData = 1;
            }
            //并网点功率因数
            if (bwdCount > powerFactorQualifiedData) {
                powerFactorQualifiedData = 0;
            }else {
                powerFactorQualifiedData = 1;
            }
            // 判断是否每个集电线都合格
            //集电线有功功率
            if (jdxCount > wireActivePowerQualifiedData) {
                wireActivePowerQualifiedData = 0;
            }else {
                wireActivePowerQualifiedData = 1;
            }


            // 并网点有功功率
            dataType = "activePower";
            // 并网点有功功率不合格
            activePowerUnqualifiedData = 1 - activePowerQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, activePowerNoData, activePowerQualifiedData, activePowerUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 并网点无功功率
            dataType = "reactivePower";
            // 并网点无功功率不合格
            reactivePowerUnqualifiedData = 1 - reactivePowerQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, reactivePowerNoData, reactivePowerQualifiedData, reactivePowerUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 并网点功率因数
            dataType = "powerFactor";
            // 并网点功率因数不合格
            powerFactorUnqualifiedData = 1 - powerFactorQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, powerFactorNoData, powerFactorQualifiedData, powerFactorUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 集电线有功功率
            dataType = "wireActivePower";
            // 集电线有功功率不合格
            wireActivePowerUnqualifiedData = 1 - wireActivePowerQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate, plantType, fileType, dataType, wireActivePowerNoData, wireActivePowerQualifiedData, wireActivePowerUnqualifiedData);
            dataList.add(fileDataCheckEntity);


            int data = fileDataCheckMapper.selectFileDataCheck(plantId, forecastDate, tableName);
            if (data == 0) {
                log.info("数据评分插入--------------"+fileName);
                fileDataCheckMapper.insertFileDataCheck(dataList, tableName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(br);
            IoUtil.close(fr);
        }
    }

    @Override
    public void GFSYZXXFileDataCheck(File file) {
        log.info("数据评分--------------------" + file.getName());
        FileDataCheckEntity fileDataCheckEntity;
        List<FileDataCheckEntity> dataList = new ArrayList<>();
        String plantId;//场站id
        Date forecastDate = null;//预测时间
        Date createDate = new Date();//创建时间
        String plantType;//场站类型
        String fileType;//文件类型
        String dataType;//字段类型

        // 并网点有功功率
        int shuntActiveNoData=0;//无数据
        int shuntActiveQualifiedData=0;//有数据且合格
        int shuntActiveQualifiedNo=0;//有数据但不合格

        // 并网点无功功率
        int reactiveNoData = 0;//无数据
        int reactiveQualifiedData = 0;//有数据且合格
        int reactiveQualifiedNo = 0;//有数据但不合格

        // 并网点功率因数
        int parallelNoData = 0;//无数据
        int parallelQualifiedData = 0;//有数据且合格
        int parallelQualifiedNo = 0;//有数据但不合格

        // 集电线有功功率
        int activeNoData = 0;//无数据
        int activeQualifiedData = 0;//有数据且合格
        int activeQualifiedNo = 0;//有数据但不合格

        // 并网点数据计数
        int bwdCount = 0;
        // 集电线数据计数
        int jdxCount = 0;
        String tableName = "HISDB.NW_NE_FILE_DATACHECK_GF_SYZXX";//表名

        // 数据计数
        int count = 0;

        // 文件名称
        String fileName = file.getName();
        String[] fileNameArr = fileName.split("_");
        // 文件类型
        fileType = fileNameArr[2];
        // 场站类型
        plantType = fileNameArr[0];
        // 场站标识
        String plantCode = fileNameArr[1].split("\\.")[1];

        //String nwCode = fileNameArr[0] + "_" + fileNameArr[1].split("\\.")[0] + ".";
        List<Map<String, Object>> idList = plantDataMapper.selectPlantData(plantCode);

        plantId = idList.get(0).get("ID").toString();
        if (plantId.contains("-")) {
            plantId = plantId.split("-")[0];
        }

        FileReader fr = null;
        BufferedReader br = null;

        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            // 每行数据
            String line = "";

            // 并网点内容数据的大小
            int bwdFileDataSize = 0;
            // 集电线内容数据的大小
            int jdxFileDataSize = 0;
            while ((line = br.readLine()) != null) {
                if (line.contains("<BWD::")) {
                    count = 1;
                    forecastDate = DataUtil.queryTime(line);
                }

                if (line.contains("<JDX::")) {
                    count = 2;
                    // 获取时间
                    forecastDate = DataUtil.queryTime(line);
                }

                if (line.contains("</BWD::") || line.contains("</JDX::")) {
                    count = 0;
                }
                if (count == 1 && line.contains("#")) {
                    bwdCount++;
                    // 按照tab分割
                    String[] strs = line.split("\t");
                    // 并网点有功功率
                    if (!("NULL".equals(strs[12]) || " ".equals(strs[12]) || "null".equals(strs[12]))) {
                        shuntActiveQualifiedData++;
                    }

                    // 并网点无功功率
                    if (!("NULL".equals(strs[13]) || " ".equals(strs[13]) || "null".equals(strs[13]))) {
                        reactiveQualifiedData++;
                    }

                    // 并网点功率因数
                    if (!("NULL".equals(strs[14]) || " ".equals(strs[14]) || "null".equals(strs[14]))) {
                        parallelQualifiedData++;
                    }
                }
                if (count == 2 && line.contains("#")) {
                    jdxCount++;
                    // 按照tab分割
                    String[] strs = line.split("\t");
                    // 集电线有功功率
                    if (!("NULL".equals(strs[4]) || " ".equals(strs[4]) || "null".equals(strs[4]))) {
                        activeQualifiedData++;
                    }
                }

                // 计算文件中逆变器、汇流箱数据量
                if (line.contains("#") && count == 1) {
                    bwdFileDataSize++;

                } else if (line.contains("#") && count == 2) {
                    jdxFileDataSize++;
                }

            }

            // 判断是否每个并网点都合格
            //并网点有功功率
            if (bwdCount > shuntActiveQualifiedData) {
                shuntActiveQualifiedData = 0;
            }else {
                shuntActiveQualifiedData = 1;
            }
            //并网点无功功率
            if (bwdCount > reactiveQualifiedData) {
                reactiveQualifiedData = 0;
            }else {
                reactiveQualifiedData = 1;
            }
            //并网点功率因数
            if (bwdCount > parallelQualifiedData) {
                parallelQualifiedData = 0;
            }else {
                parallelQualifiedData = 1;
            }
            // 判断是否每个集电线都合格
            //集电线有功功率
            if (jdxCount > activeQualifiedData) {
                activeQualifiedData = 0;
            }else {
                activeQualifiedData = 1;
            }
            // 并网点有功功率无数据
            dataType="shuntActive";
            shuntActiveQualifiedNo=1-shuntActiveQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate,
                    plantType, fileType, dataType, shuntActiveNoData, shuntActiveQualifiedData, shuntActiveQualifiedNo);
            dataList.add(fileDataCheckEntity);

            // 并网点无功功率无数据
            dataType="reactive";
            reactiveQualifiedNo =1-reactiveQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate,
                    plantType, fileType, dataType, reactiveNoData, reactiveQualifiedData, reactiveQualifiedNo);
            dataList.add(fileDataCheckEntity);

            // 并网点功率因数无数据
            dataType="parallel";
            parallelQualifiedNo =1-parallelQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate,
                    plantType, fileType, dataType, parallelNoData, parallelQualifiedData, parallelQualifiedNo);
            dataList.add(fileDataCheckEntity);

            // 集电线有功功率无数据
            dataType="active";
            activeQualifiedNo = 1-activeQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId, forecastDate, createDate,
                    plantType, fileType, dataType, activeNoData, activeQualifiedData, activeQualifiedNo);
            dataList.add(fileDataCheckEntity);

            int data = fileDataCheckMapper.selectFileDataCheck(plantId, forecastDate, tableName);
            if (data == 0) {
                log.info("数据评分插入--------------"+fileName);
                fileDataCheckMapper.insertFileDataCheck(dataList, tableName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(br);
            IoUtil.close(fr);
        }
    }
}

